/*
 * @Author: zhaoqr
 * @Date: 2024-07-05
 * @LastEditTime: 2024-07-05
 * @LastEditors: zhaoqr zhaoqr@channelsoft.com
 * @Description: 
 * @FilePath: \vue3-template\src\utils\jsonStringfy.ts
 */
const jsonStringfy = (data: any) => {
  const type = typeof data;

  if (data === undefined) { 
    return undefined;
  }

  if(type !== 'object' || data === null) {
    return String(data);
  }

  if (Array.isArray(data)) { 
    const result:string = data.map((item: any) => {;
       return jsonStringfy(item)
    }).join(',');
    return `[${result}]`;
  }

  if (type == 'object') { 
    const keys = Object.keys(data);
    const result:string = keys.map((key:any) => { 
      return `"${key}":${jsonStringfy(data[key])}`;
    }).join(',');
    return `{${result}}`;
  }

  return undefined;

}

export default jsonStringfy;
